// pages/water/devicepay/index.js
var app = getApp()
var common_Js = require("../../../component/common.js"), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
const CryptoJS = require("../../../utils/aesUtil.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceId:0,
    deviceSn:'',
    deviceInfo:{},
    isLoading: true,
    radioPayType:0,
    selectCardId:0,
    cardList:[],
    submitState: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('devicePay ', options);
    if (options.q) {
        var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
        q = "?" + q.split("?")[1];
        this.data.query = q;
        console.log('q', q)
        options = common_Js.getQueryVariable(q);
    }
    if(options.id){
      this.data.deviceId = parseInt(options.id);
    }
    if(options.sn){
      this.data.deviceSn = options.sn;
    }
    this.onHttpInit()
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/device/water/detail', {
      device_sn: that.data.deviceSn,
    }, function (res) {
      console.log("获取二维码数据", res)
      if (res.data.code == 0) {
        that.data.deviceInfo = res.data.data.device
        that.setData({
          deviceInfo: res.data.data.device
        })        
      }else{
        Toast.fail(res.data.msg);
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  onChangePayType(e){
    console.log('onChangePayType', e)
    this.data.radioPayType = parseInt(e.detail);
    this.setData({
      radioPayType: parseInt(e.detail)
    })
    if(this.data.radioPayType == 1){
      this.onHttpGetCardList();
    }
  },
  onHttpGetCardList: function () {
    var that = this;
    fetchApi.httpPost('/device/water/card/list', {
    }, function (res) {
      console.log("获取我的谁卡列表数据", res)
      if (res.data.code == 0) {
        if(res.data.totalcount == 0){
          Toast('您暂未绑定水卡');
          that.setData({
            radioPayType: 0
          })
        } else {
          that.data.selectCardId = res.data.data.cards[0].id;
          that.setData({
            selectCardId: res.data.data.cards[0].id
          })
          that.setData({
            cardList: res.data.data.cards
          })  
        }      
      }else{
        Toast.fail(res.data.msg);
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  goSubmit(){
    if(this.data.deviceInfo.onlineState == 0){
      Toast('设备已离线');
      return;
    }
    if(this.data.radioPayType == 0){//扫码打水
      this.submitOpByScan();
    }else{//水卡支付
      this.submitOpByCard();
    }
  },
  /**
   *  扫码打水
   */
  submitOpByScan:function(){
    var that = this;
    fetchApi.httpPost('/device/water/scan/order/add', {
      device_id: that.data.deviceInfo.id,
      // money: that.data.money,
    }, function (res) {
      console.log("充电订单返回数据", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({submitState : false });
        var orderId = res.data.data;        
        that.requestPay(orderId);
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  requestPay:function(order_id){
    var that = this;
    fetchApi.httpPost('/device/water/order/requestPay', {
      order_id: order_id,
      remark:that.data.remark,
      subject:'水机打水服务'
    }, function (res) {
      console.log('提交订单',res)
      if (res.data.code == 0) {
        var pay_data = res.data.data;
        var wxpayinfo = JSON.parse(res.data.data)
        console.log('pay_data',pay_data)
        if(wxpayinfo.result_code == 'SUCCESS'){
          wx.requestPayment({
            timeStamp: wxpayinfo.timeStamp,
            nonceStr: wxpayinfo.nonceStr,
            package: wxpayinfo.package,
            signType: wxpayinfo.signType,
            paySign: wxpayinfo.paySign,
            success (res) { 
              console.log('requestPayment success',res)
              wx.redirectTo({
                url: '/pages/water/payresult/index?type=0',
              })
            },
            fail (res) {
              console.log('requestPayment fail',res)
             }
          })
        }
      } else {
        that.setData({ isLoading: false, loading: true, netWork: true })
      }
    }, function () {
        that.setData({  isLoading: false,  loading: true,  netWork: true, }) 
    })
  },
  /**
   *  水卡打水
   */
  submitOpByCard:function(){
    var that = this;
    fetchApi.httpPost('/device/water/scan/order/addByCard', {
      device_id: that.data.deviceInfo.id,
      card_id: that.data.selectCardId,
      // money: that.data.money,
    }, function (res) {
      console.log("水卡打水返回数据", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({submitState : false });
        var orderId = res.data.data;
        wx.redirectTo({
          url: '/pages/water/payresult/index?type=1',
        })
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
})